﻿using System;
using System.Collections.Generic;
using System.Text;
using MarketSimulator.Utils.Attributes;
using jade.domain.FIPAAgentManagement;
using jade.domain;
using jade.core;

namespace MarketSimulator.Utils
{
    public static class YellowPages
    {
        /// <summary>
        /// Find Service
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="agentName"></param>
        /// <param name="myAgent"></param>
        /// <param name="timeOut">Miliseconds</param>
        /// <returns></returns>
        public static IList<AID> FindService(string serviceName, string agentName, Agent myAgent, int timeOut)
        {
            IList<AID> agents = new List<AID>();
            bool found = false;
            TimeSpan timeOutTimeSpan = new TimeSpan(0, 0, 0, 0, timeOut);
            DateTime t1 = DateTime.Now;
            while (!found)
            {
                var delta = DateTime.Now.Subtract(t1);
                if (delta > timeOutTimeSpan)
                {
                    break;
                }

                // search for an agent
                DFAgentDescription template = new DFAgentDescription();
                ServiceDescription sd = new ServiceDescription();
                sd.setType(serviceName);
                sd.setName(serviceName);
                template.addServices(sd);

                DFAgentDescription[] result = null;
                try
                {
                    result = DFService.search(myAgent, template);
                }
                catch (FIPAException e) { }

                if (result != null && result.Length > 0)
                {
                    if (agentName == string.Empty || agentName == null) // any solver
                    {
                        foreach (var r in result)
                        {
                            agents.Add(r.getName());
                            found = true;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < result.Length; i++)
                        {
                            // it is possible to look for a long name, not only local name
                            if (result[i].getName().getLocalName() == agentName)
                            {
                                agents.Add(result[i].getName());
                                found = true;
                                break;
                            }
                        }
                    }
                }
            }
            return agents;
        }

        /// <summary>
        /// Registers a service on behalf of an agent
        /// </summary>
        public static void RegisterService(string serviceName, Agent myAgent)
        {
            // Register the service in the yellow pages
            DFAgentDescription dfd = new DFAgentDescription();
            dfd.setName(myAgent.getAID());
            ServiceDescription sd = new ServiceDescription();
            sd.setType(serviceName);
            sd.setName(serviceName);
            dfd.addServices(sd);
            DFService.register(myAgent, dfd);
        }
        /// <summary>
        /// Deregisters a service on behalf of an agent
        /// </summary>
        public static void DeregisterService(Agent myAgent)
        {
            DFService.deregister(myAgent);
        }

    }
}
